package com.example.controller;

import com.example.domain.User;
import com.example.entity.PageResult;
import com.example.entity.Result;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping(value = {"/toMainPage"})
    public String toMainPage() {
        return "main";
    }


    @RequestMapping(value = {"/login"})  // 登录
    public String login(User user, HttpServletRequest request) {

        User userLogin = userService.login(user);

        if (userLogin == null) {// 失败
            request.setAttribute("msg", "用户名或密码错误");
            return "login";
        } else {  // 成功
            request.getSession().setAttribute("USER_SESSION", userLogin);
            return "redirect:/admin/main.jsp";
        }
    }

    // 退出登录
    @RequestMapping(value = {"/logout"})
    public String logout(HttpSession session) {
        session.invalidate();
        return "login";
    }



    /* TODO
     * 数据显示
     * 参考  books.jsp界面的显示格式
     * 这个注释以下的方法都可以修改
     * */

    @RequestMapping(value = {"/register"})
    public ModelAndView register(HttpServletRequest request, HttpSession session) {

        ModelAndView modelAndView = new ModelAndView();

        User userLogin = (User) session.getAttribute("USER_SESSION");
        Integer userId = null;
        if (userLogin.getRole().equals("USER")) {
            userId = userLogin.getId();
        }

        // 数据和视图
        int pageNum = 1;
        int pageSize = 10;
        PageResult pageResult = userService.findAllUsers(pageNum, pageSize, userId);

        modelAndView.addObject("pageResult", pageResult);
        modelAndView.setViewName("register");
        return modelAndView;
    }

    @RequestMapping(value = {"/searchUsers"})
    public ModelAndView searchRecords(User user, Integer pageNum, Integer pageSize, HttpServletRequest request) {

        // 调用service
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        User userLogin = (User) request.getSession().getAttribute("USER_SESSION");
        Integer userId = null;
        if (userLogin.getRole().equals("USER")) {
            user.setId(userLogin.getId());
        }
        PageResult pageResult = userService.search(user, pageNum, pageSize);

        // 页面
        ModelAndView modelAndView = new ModelAndView("register");

        // 数据
        // 搜索框数据回显
        modelAndView.addObject("search", user);
        // 分页信息
        modelAndView.addObject("pageResult", pageResult);
        // 当前页码数
        modelAndView.addObject("pageNum", pageNum);
        // 分页请求再次跳转的url
        modelAndView.addObject("gourl", request.getRequestURI());
        return modelAndView;
    }


    @ResponseBody
    @RequestMapping(value = {"/editUser"})
    public Result editUser(User user) {

        Integer num = userService.editUser(user);

        if (num > 0) {
            return new Result<>(true, "修改用户成功");
        } else {
            return new Result<>(true, "修改用户失败");
        }
    }

    @RequestMapping(value = {"/findById"})
    @ResponseBody
    public Result<User> findBookById(String id) {
        User user = userService.findUserById(id);
        return new Result<>(true, "", user);
    }

    @ResponseBody
    @RequestMapping(value = {"/addUser"})
    public Result addUser(User user) {

        Integer num = userService.addUser(user);

        if (num > 0) {
            // 成功
            return new Result<>(true, "新增用户成功");
        } else {
            // 失败
            return new Result<>(true, "新增用户失败");
        }

    }

}

